﻿using System;
using GDIT.BLL;
using GDIT.Base.PageBaseHelper;
using GDIT.Model;

namespace GDIT.page.manage.notice
{
    public partial class preview : System.Web.UI.Page
    {

        private BBulletinArticle bulletin = null;
        private BCollegeInformationArticle college = null;
        private Mtb_BulletinArticle bulletinDB = null;
        private Mtb_CollegeInformationArticle collegeDB = null;

        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                if (string.IsNullOrEmpty(Session["UserName"].ToString()))
                {
                    PageBase.MessageBoxShowAndGo("检查到你还没有登录，请先登录，谢谢！", "~/page/manage/login.aspx", Page);
                    return;
                }
                this.txtId.Text = Request.QueryString["id"];
                this.txtType.Text = Request.QueryString["type"];
                BindData();
            }
            catch (Exception ex)
            {
                PageBase.MessageBoxShow("操作失败，原因：" + ex.Message.ToString(), Page);
            }
        }
        //根据ID绑定文章数据
        private void BindData()
        {
            if (txtType.Text == "bulletin")//修改公告
            {
                bulletin = new BBulletinArticle();
                bulletinDB = new Mtb_BulletinArticle();
                bulletinDB = bulletin.SelectBulletinArticleById(Convert.ToInt32(txtId.Text.Trim()));
                this.BulletinLiteral.Text = bulletinDB.Contents;

            }
            else if (txtType.Text == "information")//修改资讯
            {
                college = new BCollegeInformationArticle();
                collegeDB = new Mtb_CollegeInformationArticle();
                collegeDB = college.SelectCollegeInformationArticleById(Convert.ToInt32(txtId.Text.Trim()));
                this.BulletinLiteral.Text = collegeDB.Contents;
            }
        }

        /// <summary>
        /// 开始编辑器
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnEdit_Click(object sender, EventArgs e)
        {
            try
            {
                string newUrl = string.Format("~/page/manage/notice/editor.aspx?id={0}&type={1}", this.txtId.Text, this.txtType.Text);
                Response.Redirect(newUrl);
            }
            catch (Exception ex)
            {
                PageBase.MessageBoxShow("操作失败，原因：" + ex.Message.ToString(), Page);
            }
        }

        /// <summary>
        /// 删除通知
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnDelete_Click(object sender, EventArgs e)
        {
            try
            {
                if (txtType.Text == "bulletin")//修改公告
                {
                    bulletin = new BBulletinArticle();
                    bool result = bulletin.DeleteBulletinArticle(Convert.ToInt32(txtId.Text.Trim()));
                    if (!result)
                    {
                        PageBase.MessageBoxShow("抱歉，删除公告失败！", Page);
                        return;
                    }
                    PageBase.MessageBoxShow("删除公告成功！", Page);
                }
                else if (txtType.Text == "information")//修改资讯
                {
                    college = new BCollegeInformationArticle();
                    bool result = college.DeleteCollegeInformationArticle(Convert.ToInt32(txtId.Text.Trim()));
                    if (!result)
                    {
                        PageBase.MessageBoxShow("抱歉，删除校园资讯失败！", Page);
                        return;
                    }
                    PageBase.MessageBoxShow("删除校园资讯成功！", Page);
                }
            }
            catch (Exception ex)
            {
                PageBase.MessageBoxShow("操作失败，原因是：" + ex.Message.ToString(), Page);
            }
        }
    }
}